package com.apanal.qlife.sys.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.dictionary.DictionaryMap;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.common.util.DateUtil;
import com.apanal.qlife.common.util.ExcelUtils;
import com.apanal.qlife.property.dao.JoinusMapper;
import com.apanal.qlife.property.model.Joinus;
import com.apanal.qlife.sys.dao.FeedbackMapper;
import com.apanal.qlife.sys.model.Feedback;
import com.apanal.qlife.sys.model.File;
import com.apanal.qlife.sys.service.FeedbackService;
import com.apanal.qlife.sys.service.FileService;
import com.apanal.qlife.sys.service.UserService;

/**
 * 意见反馈服务层接口实现类
 * 
 * @author shuliangxing
 * 
 * @date 2015-1-14上午11:36:08
 */
@Service
public class FeedbackServiceImpl implements FeedbackService {

	@Autowired
	FeedbackMapper mapper;

	@Autowired
	UserService userService;

	@Autowired
	JoinusMapper joinusMapper;

	@Autowired
	FileService fileService;

	@Override
	public String deleteById(Long feeId) {
		mapper.deleteByPrimaryKey(feeId);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String insert(Feedback record) {
		mapper.insert(record);

		List<File> fileList = record.getFileList();
		if (fileList != null && !fileList.isEmpty()) {
			for (File file : fileList) {
				file.setInsertBy(record.getUseId());
				file.setForeignId(record.getFeeId());
			}
			// 批量新增图片文件
			fileService.inserts(fileList);
		}

		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String insertSelective(Feedback record) {
		mapper.insertSelective(record);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public Feedback selectById(Long feeId) {
		return mapper.selectByPrimaryKey(feeId);
	}

	@Override
	public String updateById(Feedback record) {
		mapper.updateByPrimaryKey(record);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String updateByIdSelective(Feedback record) {
		mapper.updateByPrimaryKeySelective(record);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public PageReturn<Feedback> selectSysfdForPage(Feedback entity, Page page) {
		// 类型: 1:手机端反馈 2:后台反馈
		entity.setFeeType(2);
		int count = mapper.selectRelationForPageCount(entity, page);
		List<Feedback> list = mapper.selectRelationForPage(entity, page);
		for (Feedback feedback : list) {
			feedback.setUser(userService.selectRelationById(feedback.getUseId()));
			// 查询图片
			List<File> fileList = fileService.select(Constants.FEEDBACK_IMG,
					feedback.getFeeId());
			feedback.setFileList(fileList);
		}
		return new PageReturn<Feedback>(list, count, page, entity);
	}

	@Override
	public Feedback selectRelationById(Long id) {
		Feedback param = new Feedback();
		param.setFeeId(id);
		Feedback feedback = mapper.select(param).get(0);
		feedback.setUser(userService.selectRelationById(feedback.getUseId()));
		// 查询图片
		List<File> fileList = fileService.select(Constants.FEEDBACK_IMG,
				feedback.getFeeId());
		feedback.setFileList(fileList);
		return feedback;
	}

	@Override
	public void exportExcel(List<Feedback> list, String type,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		// 表头列表
		String[] titles = { "标题", "内容", "反馈类型", "反馈人", "手机号码", "角色", "所属机构",
				"反馈时间" };
		// 列宽列表
		int[] width = { 256 * 20, 256 * 40, 256 * 20, 256 * 20, 256 * 12,
				256 * 30, 256 * 20, 256 * 20 };
		// 表体数据
		List<List<String>> excelList = new ArrayList<List<String>>();
		if (list != null) {
			for (Feedback feedback : list) {
				List<String> itemList = new ArrayList<String>();
				itemList.add(feedback.getFeeTitle());
				itemList.add(feedback.getFeeInfo());
				itemList.add(DictionaryMap.getValue("FEE_THEME",
						feedback.getFeeTheme()));
				itemList.add(feedback.getUser().getUseRealName());
				itemList.add(feedback.getFeeMobile());
				itemList.add(feedback.getUser().getRolNames());
				itemList.add(feedback.getUser().getOrg().getOrgName());
				itemList.add(DateUtil.getDateSeconds(feedback.getInsertDate()));

				excelList.add(itemList);
			}
		}

		ExcelUtils.writeExcel(titles, width, excelList, "意见反馈", "意见反馈", type,
				request, response);
		return;

	}

	@Override
	public PageReturn<Feedback> findAppfdList(Feedback feedback, Page page) {
		int count = mapper.selectAppfdForPageCount(feedback, page);
		List<Feedback> list = mapper.selectAppfdForPage(feedback, page);

		return new PageReturn<Feedback>(list, count, page, feedback);
	}

	@Override
	public PageReturn<Joinus> findAppContractList(Joinus joinus, Page page) {
		int count = joinusMapper.selectAppContractForPageCount(joinus, page);
		List<Joinus> list = joinusMapper.selectAppContractForPage(joinus, page);
		return new PageReturn<Joinus>(list, count, page, joinus);
	}

}
